package com.changgou.content.controller;

import com.changgou.common.constant.StatusCode;
import com.changgou.common.vo.Result;
import com.changgou.content.pojo.Content;
import com.changgou.content.service.ContentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/****
 * @Author:www.itheima.com
 * @Description:
 * @Date www.itheima.com
 *****/

@RestController
@RequestMapping("/content")
@CrossOrigin
public class ContentController {

    @Autowired
    private ContentService contentService;

    /**
     * 根据内容分类id查询该分类的内容列表
     * @param id 分类id
     * @return 查询结果
     */
    @GetMapping("/list/category/{id}")
    public Result<List<Content>> findByCategoryId(@PathVariable Long id){
        List<Content> list = contentService.findByCategoryId(id);
        return new Result<>("查询成功！", list);
    }

    /***
     * Content分页条件搜索实现
     * @param content: 查询条件
     * @param page:当前页
     * @param size:每页显示多少条
     * @return 查询结果
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo<Content>> findPage(@RequestBody(required = false)  Content content, @PathVariable  int page, @PathVariable  int size){
        //调用ContentService实现分页条件查询Content
        PageInfo<Content> pageInfo = contentService.findPage(content, page, size);
        return new Result<PageInfo<Content>>("查询成功！", pageInfo);
    }

    /***
     * Content分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return 查询结果
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo<Content>> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用ContentService实现分页查询Content
        PageInfo<Content> pageInfo = contentService.findPage(page, size);
        return new Result<PageInfo<Content>>("查询成功！", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param content 查询条件
     * @return 查询结果
     */
    @PostMapping(value = "/search" )
    public Result<List<Content>> findList(@RequestBody(required = false)  Content content){
        //调用ContentService实现条件查询Content
        List<Content> list = contentService.findList(content);
        return new Result<List<Content>>("查询成功！", list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id 主键
     * @return 查询结果
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Long id){
        //调用ContentService实现根据主键删除
        contentService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功！");
    }

    /***
     * 修改Content数据
     * @param content 更新对象
     * @return 操作结果
     */
    @PutMapping
    public Result update(@RequestBody  Content content){
        //调用ContentService实现修改Content
        contentService.update(content);
        return new Result(true, StatusCode.OK, "修改成功！");
    }

    /***
     * 新增Content数据
     * @param content 新增对象
     * @return 操作结果
     */
    @PostMapping
    public Result add(@RequestBody  Content content){
        //调用ContentService实现添加Content
        contentService.add(content);
        return new Result(true, StatusCode.OK, "添加成功！");
    }

    /***
     * 根据ID查询Content数据
     * @param id 主键
     * @return 查询结果
     */
    @GetMapping("/{id}")
    public Result<Content> findById(@PathVariable Long id){
        //调用ContentService实现根据主键查询Content
        Content content = contentService.findById(id);
        return new Result<Content>("查询成功！", content);
    }

    /***
     * 查询Content全部数据
     * @return 查询结果
     */
    @GetMapping
    public Result<List<Content>> findAll(){
        //调用ContentService实现查询所有Content
        List<Content> list = contentService.findAll();
        return new Result<List<Content>>("查询成功！", list);
    }
}
